﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using Data.UserDb;
using System.Data.Linq;

namespace Acc.Lib.Providers
{
     
    public class ActionLoader
    {
        private static ActionProviderCollection _objProviders = null;
        private static readonly object Lock = new object();

        
        private static void Load()
        {
            if (_objProviders == null)
            {
                lock (Lock)
                {
                    var objSection = (ActionProviderSection) WebConfigurationManager.GetSection("actionProvider");
                    _objProviders = new ActionProviderCollection();
                    ProvidersHelper.InstantiateProviders(objSection.Providers, _objProviders, typeof (ActionProvider));

                }
            }
        }

        public static ActionProvider GetSrv(Int32 srvId)
        {
            Load();
            return _objProviders[srvId.ToString()];
        }

        public static Boolean SrvOrder(Int64 orderId)
        {
            var sr = new ShopRepository();
            var order = sr.GetOrder(orderId);
            if (order == null) throw new Exception(String.Format("订单Id:{0}不存在", orderId));

            var provider = GetSrv(order.SrvId);

            return provider.GenericServ(order, sr);
        }

        //public static IQueryable<ActionProvider> GetProvdersInfo()
        //{
        //    Load();
        //    return _objProviders.AsQueryable() as IQueryable<ActionProvider>;
        //}

        public static IEnumerable<ActionProvider> GetAllProvider()
        {
            Load();
            var list = new List<ActionProvider>();
            for(var i = 0; i < _objProviders.Count; i++)
            {
                list.Add(_objProviders[i.ToString()]);
            }

            return from p in list where p.ServId > 0 select p;
        }
    }
}
